package cn.tedu.blackhorse.service.impl;

import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.AdminMapper;
import cn.tedu.blackhorse.mapper.PermissionMapper;
import cn.tedu.blackhorse.pojo.dto.PermissionAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.PermissionUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.Permission;
import cn.tedu.blackhorse.pojo.vo.PermissionListItemVO;
import cn.tedu.blackhorse.pojo.vo.PermissionStandardVO;
import cn.tedu.blackhorse.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public void addNew(PermissionAddNewDTO permissionAddNewDTO) {
        int count = permissionMapper.countByName(permissionAddNewDTO.getName());
        if (count != 0){
            String message = "添加失败,权限名已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddNewDTO,permission);
        int row = permissionMapper.insert(permission);
        if (row != 1){
            String message = "添加失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        PermissionStandardVO standard = permissionMapper.getById(id);
        if (standard == null){
            String message = "删除失败,要删除的权限不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int count = adminMapper.countByPermissionId(id);
        if (count != 0){
            String message = "删除失败,该权限还有关联的管理员";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int row = permissionMapper.deleteById(id);
        if (row != 1){
            String message = "删除失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(Long id,PermissionUpdateDTO permissionUpdateDTO) {
        PermissionStandardVO standard = permissionMapper.getById(id);
        if (standard == null){
            String message = "修改失败,要修改的权限不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionUpdateDTO,permission);
        permission.setId(id);
        int row = permissionMapper.updateById(permission);
        if (row != 1){
            String message = "修改失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public List<PermissionListItemVO> list() {
        return permissionMapper.list();
    }
}
